import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shopping_group/base/base_constant.dart';
import 'package:shopping_group/entity/widget_action_entity.dart';
import 'package:shopping_group/event/event.dart';
import 'package:shopping_group/modle/person/bloc/my_address_bloc.dart';
import 'package:shopping_group/modle/person/entity/soujianren_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:shopping_group/res/dimens.dart';

import 'package:shopping_group/utils/route_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';

import 'add_adress_page.dart';

class MyAdressPage extends StatefulWidget {
  final String type;
  MyAdressPage({Key key, this.type}) : super(key: key);

  @override
  _MyAdressPageState createState() => _MyAdressPageState();
}

class _MyAdressPageState extends State<MyAdressPage> {
  List<SouJianRenEntity> _listEntity = [];
  List<WidgetActionEntity> _actionList = [];
  StreamSubscription _refresListSubscription;
  MyAddressBloc _bloc;
  @override
  void initState() {
    super.initState();
    _actionList
      ..add(WidgetActionEntity()
        ..actionType = 'text'
        ..title = '新增地址'
        ..titleSize = Dimens.FONT_SIZE_14
        ..actionColor = Colours.title_white);
    _bloc = MyAddressBloc();

    _bloc.initDate.add(null);

    _refresListSubscription = eventBus.on<SaveAddressEvent>().listen((event) {
      _listEntity.clear();
      _bloc.initDate.add(null);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WidgetUtils.showAppar(context, '我的地址',
          actionList: _actionList,
          textColor: Colors.white,
          isHide: false, onTapCall: (e) {
        RouteUtil.goParameter(AddAdressPage(
          action: '新增地址',
        ));
      }),
      body: Container(
        decoration: new BoxDecoration(
          color: Colours.gray_f0,
        ),
        child: Stack(
          children: <Widget>[
            _showlistView(),
          ],
        ),
      ),
    );
  }

  Widget _showlistView() {
    return StreamBuilder(
      stream: _bloc.sendinitData,
      initialData: _listEntity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is List) {
          _listEntity = object;
        }
        int count = _listEntity.length;
        if (0 != count) {
          return Container(
            decoration: new BoxDecoration(
              color: Colors.white,
            ),
            child: ListView.builder(
              shrinkWrap: true,
              padding: const EdgeInsets.only(top: 4),
              itemCount: _listEntity.length,
              itemBuilder: buildItem,
            ),
          );
        } else {
          return Container(
            alignment: Alignment.center,
            child: WidgetUtils.emptyView(
              contexts,
            ),
          );
        }
      },
    );
  }

  //ListView的Item
  Widget buildItem(BuildContext context, int index) {
    return GestureDetector(
      onTap: () {
        if (BaseConstant.SELECT_ADRESS == this.widget.type) {
          eventBus.fire(SelectOSouJianRenEvent(_listEntity[index]));
          RouteUtil.closeView();
          return;
        }
        setState(() {
          _listEntity.forEach((element) {
            element.isCilke = false;
            _listEntity[index].isCilke = true;
          });
        });
      },
      child: Container(
        color: Colors.transparent,
        child: _xunchaItem(_listEntity[index]),
      ),
    );
  }

  Widget _xunchaItem(SouJianRenEntity entity) {
    return Column(
      children: <Widget>[
        Row(
          children: <Widget>[
            SizedBox(
              width: 8,
            ),
            // BaseConstant.SELECT_ADRESS == this.widget.type
            //     ? Container()
            //     : Icon(
            //         entity.isCilke
            //             ? Icons.radio_button_checked
            //             : Icons.radio_button_unchecked,
            //         color: Colours.themeColor,
            //         size: 18,
            //       ),
            Expanded(
                child: Container(
              padding: const EdgeInsets.symmetric(vertical: 12),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16),
                    child: Row(
                      children: <Widget>[
                        WidgetUtils.getTextColorView(
                          entity.souJianRenName,
                          Colours.text_2222,
                          Dimens.FONT_SIZE_16,
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        WidgetUtils.getTextColorView(
                          entity.souJianRenPhone,
                          Colours.text_2222,
                          Dimens.FONT_SIZE_16,
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(
                      left: 16,
                      right: 16,
                      top: 8,
                    ),
                    child: WidgetUtils.getTextColorView(
                      entity.souJianRenAddress,
                      Colours.gray_91,
                      Dimens.FONT_SIZE_15,
                    ),
                  ),
                  // Padding(
                  //   padding: const EdgeInsets.only(
                  //     left: 16,
                  //     right: 16,
                  //     top: 8,
                  //   ),
                  //   child: WidgetUtils.showBoxBotton(entity.content,
                  //       boxColor: Colours.blackground_609,
                  //       textColor: Colours.title_white,
                  //       textSize: 14,
                  //       borderColor: Colours.blackground_609,
                  //       borderRadius: 32),
                  // ),
                ],
              ),
            )),
            BaseConstant.SELECT_ADRESS == this.widget.type
                ? Container()
                : Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16),
                    child: Row(
                      children: <Widget>[
                        GestureDetector(
                          onTap: () {
                            RouteUtil.goParameter(AddAdressPage(
                              entity: entity,
                              action: '修改地址',
                            ));
                          },
                          child: Column(
                            children: <Widget>[
                              Icon(
                                Icons.edit,
                                color: Colours.themeColor,
                                size: 24,
                              ),
                              WidgetUtils.getTextColorView(
                                  '编辑', Colours.text_2222, Dimens.FONT_SIZE_16),
                            ],
                          ),
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 8),
                        ),
                        GestureDetector(
                          onTap: () {
                            _bloc.deleteDate.add(entity);
                          },
                          child: Column(
                            children: <Widget>[
                              Icon(
                                Icons.delete,
                                color: Colours.themeColor,
                                size: 24,
                              ),
                              WidgetUtils.getTextColorView(
                                  '删除', Colours.text_2222, Dimens.FONT_SIZE_16),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
          ],
        ),
        WidgetUtils.drawCodeDivider(height: 4),
      ],
    );
  }

  @override
  void dispose() {
    _refresListSubscription?.cancel();
    super.dispose();
  }
}
